/**********************************************************************
 Copyright (C) 2010 Teppo Eeva

 This file is part of Arithmetic Game.
 For more information, see <http://code.google.com/p/arithmetic-game/>

 Arithmetic Game is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Arithmetic Game is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Arithmetic Game.  If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#include "settingsdialog.h"
#include "ui_settingsdialog.h"

#include <QDebug>
#include <QTranslator>
#include <QDesktopWidget>
#include <arithgame.h>


SettingsDialog::SettingsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingsDialog),
    m_settings(settingsName)
{
    ui->setupUi(this);

#ifdef Q_OS_SYMBIAN
    QRect screenGeometry = QApplication::desktop()->screenGeometry();
    resize( screenGeometry.width(), screenGeometry.height() - 24 );
#endif // Q_OS_SYMBIAN
    
    connect( ui->buttonBox, SIGNAL(accepted()), this, SLOT(storeSettings()));
    connect( ui->buttonBox, SIGNAL(rejected()), this, SLOT(initSettings()));
    initSettings();
    ui->soundEffectsCheckBox->setFocus(Qt::TabFocusReason);
}

SettingsDialog::~SettingsDialog()
{
    delete ui;
}

QLocale::Language SettingsDialog::gameLanguage()
{
    return (QLocale::Language)(m_settings.value("settings/language",31).toInt());
}

ArithDiffLevel SettingsDialog::difficultyLevel()
{
    return (ArithDiffLevel)(m_settings.value("settings/difficulty", 1).toInt());
}

bool SettingsDialog::isSoundsEnabled()
{
     return m_settings.value("settings/sounds", false).toBool();
}

void SettingsDialog::initSettings()
{
    // Read difficulty level
    ArithDiffLevel diffLevel = difficultyLevel();

    switch (diffLevel)
    {
    case Easy:
        ui->easyLevelRadioButton->setChecked(true);
        break;
    case Hard:
        ui->hardLevelRadioButton->setChecked(true);
        break;
    case Moderate:
    case NotDefined:
    default:
        ui->moderateLevelRadioButton->setChecked(true);
        break;
    }
    emit difftLevelOptionUpdated(diffLevel);

    // Read selected language
    QLocale::Language lang = gameLanguage();

    switch(lang)
    {
    case QLocale::Finnish:
        ui->finnishRadioButton->setChecked(true);
        break;
    case QLocale::English:
        ui->englishRadioButton->setChecked(true);
        break;
    default:
        if (QLocale::system().language() == QLocale::Finnish)
        {
            ui->finnishRadioButton->setChecked(true);
            lang = QLocale::Finnish;
        }
        else
        {
            ui->englishRadioButton->setChecked(true);
            lang = QLocale::English;
        }
        break;
    }
    localize(lang);


    // Read whether sound effects are enabled or not
    bool soundsEnabled = isSoundsEnabled();
    ui->soundEffectsCheckBox->setChecked(soundsEnabled);
    emit audioEffectOptionUpdated(soundsEnabled);

    qDebug() << "SettingsDialog::InitSettings - Settings Initilized";
    qDebug() << "Difficulty: " << diffLevel << " Language: " << lang << " Sounds: " << soundsEnabled;

}

void SettingsDialog::storeSettings()
{
    // Store difficulty level
    ArithDiffLevel diffLevel = ui->easyLevelRadioButton->isChecked() ? Easy :
        ui->moderateLevelRadioButton->isChecked() ? Moderate : Hard;
    m_settings.setValue("settings/difficulty" ,diffLevel);
    emit difftLevelOptionUpdated(diffLevel);

    // Store language
    QLocale::Language lang = ui->finnishRadioButton->isChecked() ? QLocale::Finnish : QLocale::English;
    m_settings.setValue("settings/language", lang);
    localize(lang);

    // Store sound effect setting
    bool soundsEnabled = ui->soundEffectsCheckBox->isChecked();
    m_settings.setValue("settings/sounds", soundsEnabled);
    emit audioEffectOptionUpdated(soundsEnabled);

    qDebug() << "SettingsDialog::StoreSettings - Settings Stored";
    qDebug() << "Difficulty: " << diffLevel << " Language: " << lang << " Sounds: " << soundsEnabled;
}

void SettingsDialog::changeEvent(QEvent *e)
{
/*    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
*/}

void SettingsDialog::localize(QLocale::Language lang)
{
Q_UNUSED(lang);
    /*    QTranslator arithGameTranslator;

    if (lang == QLocale::Finnish)
    {
        qDebug() << "Localize - FI";
        arithGameTranslator.load("arithgame_fi",qApp->applicationDirPath());
    }
    else
    {
        qDebug() << "Localize - EN";
        arithGameTranslator.load("arithgame_en",qApp->applicationDirPath());
    }
    qApp->installTranslator(&arithGameTranslator);

  emit languageOptionUpdated();
*/
}
